import { Controller, Get, Post, Req, Res, UseGuards } from '@nestjs/common';
import { Request, Response } from 'express'
import { AdminService } from './admin.service';
import { Admin } from './admin.entity';
import { AuthService } from 'src/auth/auth.service';

@Controller('admin')
export class AdminController {
    constructor(private adminService: AdminService, private readonly authService: AuthService) { }

    @Get("/findAll")
    async findAll(): Promise<Admin[]> {
        return this.adminService.findAll();
        // return await null;
    }

    @Post("/login")
    async findOne(@Req() req: Request, @Res({ passthrough: true }) res: Response): Promise<string> {
        const admin = this.adminService.findByUserNameAndPassword(req.body["username"], req.body["password"]);
        if ((await admin) !== null) {   // 存在该用户，颁发签证
            const token = this.authService.certificate((await admin));
            res.cookie('token', (await token), { maxAge: 1000 * 60 * 60 * 24, httpOnly: false });
            return (await admin).username
        }
        return null;
    }
}
